package com.wwwzh.himalayafm.adapter.live_adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.callback.IsUpListener;
import com.wwwzh.himalayafm.entity.entity_live.Categories;

import java.util.List;

/**
 * Created by wuruyuan on 2016/12/6.
 */

public class GridAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<Categories> data;
    private LayoutInflater inflater;
    private List<Categories> list;
    private IsUpListener listener;


    public GridAdapter(Context context, List<Categories> data, List<Categories> list, IsUpListener listener) {
        this.context = context;
        this.data = data;
        this.list = list;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
//        Log.d("1620", "data.size(): "+data.size());
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        {
            if (position != data.size() - 1) {
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.grid_item, parent, false);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.tv.setText(data.get(position).getName());
//                holder.tv.setOnClickListener(this);
                return convertView;
            } else {
                if (data.size() == 8) {
                    convertView = inflater.inflate(R.layout.image_item, parent, false);
                    holder = new ViewHolder(convertView);
                    holder.image.setOnClickListener(this);
                    return convertView;
                } else if (data.size() == 16) {
                    convertView = inflater.inflate(R.layout.image_up_item, parent, false);
                    holder = new ViewHolder(convertView);
                    holder.imagedwn.setOnClickListener(this);
                    return convertView;
                } else {
                    return convertView;
                }
            }

        }
    }

    @Override
    public void onClick(View v) {
        Log.d("1620", "点击了" + "123");
        switch (v.getId()) {
            case R.id.image_up:
                data.clear();
                data.addAll(list);
                data.add(list.get(0));
                listener.isUp(data);
                break;
            case R.id.image_down:
                data.clear();
                for (int i = 0; i < 8; i++) {
                    data.add(i,list.get(i));
                }
                listener.isUp(data);
                break;
            case R.id.grid_tv:
                break;
        }

    }

    private static class ViewHolder {
        TextView tv;
        ImageView image;
        ImageView imagedwn;

        public ViewHolder(View convertView) {
            tv = (TextView) convertView.findViewById(R.id.grid_tv);
            image = (ImageView) convertView.findViewById(R.id.image_up);
            imagedwn = (ImageView) convertView.findViewById(R.id.image_down);
        }
    }
}
